{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1e774a0-0f1f-4dda-884d-1f3e051c5a6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "import os\n",
    "import json\n",
    "import requests\n",
    "import jsonlines\n",
    "from tqdm import tqdm\n",
    "import time\n",
    "from multiprocessing import Pool\n",
    "from functools import partial\n",
    "\n",
    "from tenacity import (\n",
    "    retry,\n",
    "    stop_after_attempt,\n",
    "    wait_random_exponential,\n",
    "    RetryError\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b6a1bf0-4748-4b63-8429-176dec4d6250",
   "metadata": {},
   "outputs": [],
   "source": [
    "MAX_API_RETRY = 10\n",
    "LLM_MIT_RETRY_SLEEP = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "328ab59d-58fa-41ec-a7c9-2f09b6c7229f",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = list(jsonlines.open(\"../data/wiki_roleplay_multilingual_test_input_w_evidence.jsonl\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4daec27-d547-4a2e-9ef4-1a34f199971a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# model = \"mistralai/Mixtral-8x7B-Instruct-v0.1\"\n",
    "# model = \"openchat/openchat-3.5-1210\"\n",
    "model = \"mistralai/Mistral-7B-Instruct-v0.2\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5038a0e0-5475-41f7-8e89-c63fefe85f81",
   "metadata": {},
   "outputs": [],
   "source": [
    "@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(5))\n",
    "def completion_with_backoff(client, **kwargs):\n",
    "    try:\n",
    "        return client.chat.completions.create(**kwargs)\n",
    "    except Exception as e:\n",
    "        print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4aab4850-ddba-4a79-a2b3-8dd0730c46ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "def process(item, model):\n",
    "    client = openai.OpenAI(\n",
    "        api_key=\"your together api key\",\n",
    "        base_url='https://api.together.xyz',\n",
    "    )\n",
    "    \n",
    "    message = [{\"role\": \"system\", \"content\": item['system']}]\n",
    "    try:\n",
    "        for prompt in item['prompts']:\n",
    "            message.append({\"role\": \"user\", \"content\": prompt})\n",
    "            answer = completion_with_backoff(client, messages=message, model=model, max_tokens=8192)\n",
    "            answer = answer.choices[0].message.content\n",
    "            message.append({\"role\": \"assistant\", \"content\": answer})\n",
    "    except Exception as e:\n",
    "        pass\n",
    "    \n",
    "    if len(message) == 1:\n",
    "        return None\n",
    "    else:\n",
    "        item['messages'] = message\n",
    "        return item\n",
    "    \n",
    "func = partial(process, model=model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e39e1e7e-32b9-4fa6-9670-2dc834b82a6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "results = []\n",
    "with Pool(32) as p:\n",
    "    pbar = tqdm(total=len(data))\n",
    "    for item in p.imap_unordered(func, data):\n",
    "        pbar.update(1)\n",
    "        results.append(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec9785b2-16ef-4895-ba64-acddc314dfc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "with jsonlines.open(f\"data/results/wiki_roleplay_multilingual_test_input_w_evidence_{model.split('/')[1]}.jsonl\", \"w\") as f:\n",
    "    for each in results:\n",
    "        f.write(each)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
